<template>
  <div class="login">
    <el-form ref="form" :rules="rules" :model="form" class="myform">
      <h3>iHRM 后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          show-password
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          class="btn"
          type="primary"
          @click="loginClick"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      loading: false,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '手机号不合法',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              const bool = reg.test(value)
              bool ? callback() : callback(new Error('验证失败'))
            }
          }
        ],
        password: [
          {
            required: true,
            message: '密码不合法',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions({
      login: 'user/login'
    }),
    loginClick () {
      this.$refs.form.validate(async result => {
        if (result) {
          try {
            this.loading = true
            await this.login(this.form)
            this.loading = false
            this.$message({
              message: '欢迎回家',
              type: 'success'
            })
            const _redirect = this.$route.query._redirect
            if(_redirect){
              this.$router.push(_redirect)
            }else{
              this.$router.push('/')
            }
          } catch (error) {
            this.loading = false
            console.log(error.message)
          }
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login {
  background-image: url('../../assets/common/login.jpg');
  background-position: center;
  height: 100%;
  .myform {
    width: 520px;
    height: 400px;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -70%);
    box-sizing: border-box;
    padding: 38px;
    padding-top: 0;
    h3 {
      font-size: 48px;
      font-weight: 200;
      letter-spacing: 1px; // 设置文本之间的间隔
      color: #fff;
      line-height: 1;
    }
    input.el-input__inner {
      height: 50px;
      background-color: rgba(255, 255, 255, 0.7);
      border: none;
      color: #68b0fe;
      border-radius: 6px;
      padding-left: 40px;
    }
    i.el-input__icon {
      color: #666;
    }
    .btn {
      font-size: 20px;
      width: 100%;
      height: 60px;
      background-color: #407ffe;
      border-radius: 6px;
    }
  }
}
</style>
